<script setup lang="ts">
import KSection from "@/views/domain/kubeHud/views/components/k-section/index.vue";
import KoDataItem from "@/views/domain/kubeHud/views/components/ko-data-item/index.vue";
import KoBase from "@/views/domain/kubeHud/views/components/ko-workloads/ko-base.vue";
import koCheckRow from "@/views/domain/kubeHud/views/components/ko-workloads/ko-check-row.vue";

import KoKvTable from "@/views/domain/kubeHud/views/components/ko-workloads/ko-kv-table.vue";

import KoSpecBase from "@/views/domain/kubeHud/views/components/ko-workloads/ko-spec/ko-spec-base.vue";
import KoNodeSelector from "@/views/domain/kubeHud/views/components/ko-workloads/ko-spec/ko-node-selector.vue";
import KoPodScheduling from "@/views/domain/kubeHud/views/components/ko-workloads/ko-spec/ko-pod-scheduling.vue";
import KoTolerations from "@/views/domain/kubeHud/views/components/ko-workloads/ko-spec/ko-tolerations.vue";
import KoNetworking from "@/views/domain/kubeHud/views/components/ko-workloads/ko-spec/ko-networking.vue";
import KoSpecSecurity from "@/views/domain/kubeHud/views/components/ko-workloads/ko-spec/ko-spec-security.vue";
import KoUpgradeCommon from "@/views/domain/kubeHud/views/components/ko-workloads/ko-spec/ko-upgrade-common.vue";
import KoUpgradeJob from "@/views/domain/kubeHud/views/components/ko-workloads/ko-spec/ko-upgrade-job.vue";

import KoVolume from "@/views/domain/kubeHud/views/components/ko-workloads/ko-volume.vue";
import KoVolumeClaim from "@/views/domain/kubeHud/views/components/ko-workloads/ko-volume-claim.vue";
import KoContainer from "@/views/domain/kubeHud/views/components/ko-workloads/ko-container.vue";
import KoContainerEdit from "@/views/domain/kubeHud/views/components/ko-workloads/ko-container-edit.vue";
import KoContainerRow from "@/views/domain/kubeHud/views/components/ko-workloads/ko-container-row.vue";
import KoPorts from "@/views/domain/kubeHud/views/components/ko-workloads/ko-ports.vue";
import KoCommand from "@/views/domain/kubeHud/views/components/ko-workloads/ko-command.vue";
import KoEnvironment from "@/views/domain/kubeHud/views/components/ko-workloads/ko-environment.vue";
import KoResources from "@/views/domain/kubeHud/views/components/ko-workloads/ko-resources.vue";
import KoHealthCheck from "@/views/domain/kubeHud/views/components/ko-workloads/ko-health-check.vue";
import KoSecurityContext from "@/views/domain/kubeHud/views/components/ko-workloads/ko-security-context.vue";
import KoVolumeMount from "@/views/domain/kubeHud/views/components/ko-workloads/ko-volume-mount.vue";
import KoServiceAdd from "@/views/domain/kubeHud/views/components/ko-workloads/ko-service/ko-service-add.vue";

import {
  ConfigMap,
  Container,
  ObjectMeta,
  Secret,
  Volume
} from "@/views/domain/kubeHud/api/k8s";
import { WorkloadEditContainerType } from "./types";
import { ref } from "vue";

type Prop = {
  namespace?: string;
  readonly?: boolean;
  type?: WorkloadEditContainerType;
  createTrigger?: boolean;
  secretList?: Secret[];
  configMapList?: ConfigMap[];
  volumeList?: Volume[];
};

const props = defineProps<Prop>();
const emits = defineEmits(["save"]);
const containerType = ref<WorkloadEditContainerType>(
  props.type || WorkloadEditContainerType.Standard
);

const container = defineModel<Container>("container");
const workloadMetadata = defineModel<ObjectMeta>("workloadMetadata");

const onSubmit = () => {
  emits("save", container.value, containerType.value);
};

const healthCheckToggle = ref(
  !!(
    container.value.readinessProbe ||
    container.value.livenessProbe ||
    container.value.startupProbe
  )
);

const onHandleHealthCheckToggle = (v: boolean) => {
  healthCheckToggle.value = v;
  if (!healthCheckToggle.value) {
    container.value.readinessProbe = undefined;
    container.value.livenessProbe = undefined;
    container.value.startupProbe = undefined;
  } else {
    if (!container.value.readinessProbe) {
      container.value.readinessProbe = {};
    }
    if (!container.value.livenessProbe) {
      container.value.livenessProbe = {};
    }
    if (!container.value.startupProbe) {
      container.value.startupProbe = {};
    }
  }
};
</script>

<template>
  <div class="container-edit-form">
    <k-section :title="$t('business.workload.general')">
      <ko-container
        v-model:container="container"
        v-model:metadata="workloadMetadata"
        :readonly="readonly"
        :containerType="type"
        :secret-list="secretList"
      />

      <ko-resources
        v-model:resource="container.resources"
        :readonly="readonly"
      />
    </k-section>

    <k-section
      :title="$t('business.workload.command')"
      check
      check-on-body
      collapse
      :check-model="container.command"
      @check-changed="v => (container.command = v ? [] : undefined)"
    >
      <ko-command v-model:container="container" :readonly="readonly" />
    </k-section>

    <k-section
      :title="$t('business.workload.port')"
      check
      check-on-body
      collapse
      :check-model="container.ports"
      @check-changed="v => (container.ports = v ? [] : undefined)"
    >
      <ko-ports v-model:container="container" :readonly="readonly" />
    </k-section>

    <k-section
      :title="$t('business.workload.environment_variable')"
      check
      collapse
      check-on-body
      :check-model="container.env || container.envFrom"
      @check-changed="
        v => (
          (container.env = v ? [] : undefined),
          (container.envFrom = v ? [] : undefined)
        )
      "
    >
      <ko-environment
        v-model:container="container"
        :namespace="namespace"
        :readonly="readonly"
        :current-namespace="workloadMetadata.namespace"
        :config-map-list="configMapList"
        :secret-list="secretList"
      />
    </k-section>
    <k-section
      :title="$t('business.workload.health_check')"
      check
      check-on-body
      collapse
      :check-model="healthCheckToggle"
      @check-changed="onHandleHealthCheckToggle"
    >
      <ko-health-check
        v-model:probe="container.livenessProbe"
        :readonly="readonly"
        health-check-type="liveness"
        @delete="container.livenessProbe = undefined"
      />
      <ko-health-check
        v-model:probe="container.readinessProbe"
        :readonly="readonly"
        health-check-type="readiness"
        class="mt-2 mb-2"
        @delete="container.readinessProbe = undefined"
      />
      <ko-health-check
        v-model:probe="container.startupProbe"
        :readonly="readonly"
        health-check-type="startup"
        @delete="container.startupProbe = undefined"
      />
    </k-section>

    <k-section
      :title="$t('business.workload.pod_security_context')"
      check
      collapse
      check-on-body
      :check-model="container.securityContext"
      @check-changed="
        v =>
          (container.securityContext = v
            ? { capabilities: {}, seLinuxOptions: {} }
            : undefined)
      "
    >
      <ko-security-context
        v-model:security-context="container.securityContext"
        :readonly="readonly"
      />
    </k-section>

    <k-section
      v-if="false"
      :title="$t('business.workload.mount')"
      check
      collapse
      check-on-body
      :check-model="container.volumeMounts"
      @check-changed="v => (container.volumeMounts = v ? [] : undefined)"
    >
      <ko-volume-mount
        v-model:container="container"
        :readonly="readonly"
        :volume-list="volumeList"
      />
    </k-section>
  </div>
</template>
<style lang="scss" scoped>
.container-edit-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
  padding: 5px 10px;
}
</style>
